Ext.define('com.hh.global.BaseSimpleTreePanel', {
	extend : 'com.hh.base.BasePanel',
	region : 'west',
	layout : 'fit',
	split : true,
	collapsible : true,
	border : true,
	padding : '1 1 1 1',
	title : null,
	query_action : null,
	delete_action : null,
	editPage : null,
	beInit : function() {
	},
	afInit : function() {
	},
	init : function() {
		this.beInit();
		var tree = this.createTreePanel();
		this.tree = tree;
		this.add(tree);
		this.afInit();
	},
	rightMenuItems : [],
	toolbarItems : [],
	getRightMenuItems : function() {
		var toolbarItems = [];
		for ( var i = 0; i < this.toolbarItems.length; i++) {
			toolbarItems.push(this.toolbarItems[i]);
		}
		for ( var i = 0; i < this.rightMenuItems.length; i++) {
			toolbarItems.push(this.rightMenuItems[i]);
		}
		if (this.editPage != null) {
			toolbarItems.push(this.getToolbarItem("add"));
			toolbarItems.push(this.getToolbarItem("addRoot"));
			toolbarItems.push(this.getToolbarItem("update"));
			toolbarItems.push(this.getToolbarItem("delete"));
		}

		toolbarItems.push(this.getToolbarItem("expandAll"));
		toolbarItems.push(this.getToolbarItem("collapseAll"));
		toolbarItems.push(this.getToolbarItem("refresh"));

		return toolbarItems;
	},
	getRightMenu : function() {
		var panel = this;
		this.rightMenu = Ext.create('Ext.menu.Menu', {
			items : panel.getRightMenuItems()
		});

		return this.rightMenu;
	},
	containerRightMenuItems : [],
	getContainerRightMenuItems : function() {
		var toolbarItems = [];
		for ( var i = 0; i < this.toolbarItems.length; i++) {
			toolbarItems.push(this.toolbarItems[i]);
		}
		for ( var i = 0; i < this.containerRightMenuItems.length; i++) {
			toolbarItems.push(this.containerRightMenuItems[i]);
		}
		if (this.editPage != null) {
			toolbarItems.push(this.getToolbarItem("add"));
			toolbarItems.push(this.getToolbarItem("addRoot"));
		}
		toolbarItems.push(this.getToolbarItem("expandAll"));
		toolbarItems.push(this.getToolbarItem("collapseAll"));
		toolbarItems.push(this.getToolbarItem("refresh"));

		return toolbarItems;
	},
	getContainerRightMenu : function() {
		var panel = this;
		this.rightMenu = Ext.create('Ext.menu.Menu', {
			items : panel.getContainerRightMenuItems()
		});
		return this.rightMenu;
	},
	createTreePanel : function() {
		this.store = this.getTreeStore();
		this.tree = Ext.create('com.hh.base.BaseTreePanel', {
			tbar : this.getTbar(),
			bbar : this.getBbar(),
			store : this.getTreeStore(),
			border : false,
			listeners : this.getTreePanelListeners(),
			collapsible : false
		});
		var rightMenu = this.getRightMenu();
		this.tree.on('itemcontextmenu', function(a, b, c, d, e) {
			rightMenu.showAt(e.getXY());
			rightMenu.doConstrain();
			e.stopEvent();
		}, this);

		var containeRrightMenu = this.getContainerRightMenu();
		this.tree.on('containercontextmenu', function(view, e) {
			containeRrightMenu.showAt(e.getXY());
			containeRrightMenu.doConstrain();
			e.stopEvent();
		}, this);
		return this.tree;
	},
	getTreePanelListeners : function() {

	},
	extraParams : {},
	getExtraParams : function() {
		return this.extraParams;
	},
	setExtraParams : function(extraParams) {
		this.extraParams = extraParams;
	},
	getTreeStore : function() {
		var page = this;
		this.store = Ext.create('com.hh.base.BaseTreeStore', {
			root : {},
			proxy : {
				type : 'ajax',
				url : page.query_action,
				extraParams : this.getExtraParams()
			}
		});
		return this.store;
	},
	getBbar : function() {
		var page = this;
		return Ext.create('com.hh.base.BaseToolbar', {
			items : page.getBbarItems()
		});
	},
	getBbarItems : function() {
		return [ this.getToolbarItem("refresh") ];
	},
	getToolbarItem : function(type) {
		var panel = this;
		if (type == 'add') {
			return {
				iconCls : 'add',
				text : '添加',
				handler : function() {
					panel.doAdd();
				}
			};
		} else if(type == 'addRoot'){
			return {
				iconCls : 'add',
				text : '添加根节点',
				handler : function() {
					panel.doAddRoot();
				}
			};
		}else if (type == 'delete') {
			return {
				iconCls : 'delete',
				text : '删除',
				handler : function() {
					panel.doDelete();
				}
			};
		} else if (type == 'update') {
			return {
				iconCls : 'update',
				text : '编辑',
				handler : function() {
					panel.doUpdate();
				}
			};
		} else if (type == 'expandAll') {
			return {
				iconCls : 'expand',
				text : '全部展开',
				handler : function() {
					panel.tree.expandAll();
				}
			};
		} else if (type == 'collapseAll') {
			return {
				iconCls : 'collapse',
				text : '全部收缩',
				handler : function() {
					panel.tree.collapseAll();
				}
			};
		} else if (type == "refresh") {
			return {
				iconCls : 'table_refresh',
				text : '刷新',
				handler : function() {
					panel.store.load();
				}
			};
		}
	},
	getTbarItems : function() {
		var toolbarItems = [];
		for ( var i = 0; i < this.toolbarItems.length; i++) {
			toolbarItems.push(this.toolbarItems[i]);
		}
		if (this.editPage != null) {
			toolbarItems.push(this.getToolbarItem("add"));
			toolbarItems.push(this.getToolbarItem("addRoot"));
			toolbarItems.push(this.getToolbarItem("update"));
			toolbarItems.push(this.getToolbarItem("delete"));
		}
		
		toolbarItems.push(this.getToolbarItem("expandAll"));
		toolbarItems.push(this.getToolbarItem("collapseAll"));
		return toolbarItems;
	},
	getTbar : function() {
		var panel = this;
		return Ext.create('com.hh.base.BaseToolbar', {
			enableOverflow : true,
			items : panel.getTbarItems()
		});
	},
	doAddRoot : function() {
		var page = this;
		var editPageUrl = this.editPage;
		var win = ExtUtil.create(editPageUrl, {
			id : this.id + '-' + editPageUrl.replace(/\./g, ''),
			addType : 'root',
			parentPanel : this,
			callbackRefresh : function() {
				page.store.load();
			}
		});
		win.show();
	},
	doAdd : function() {
		var page = this;
//		var record = page.tree.getSelectionModel().getSelection()[0];
		var record =	page.tree.getSelectionModel().lastFocused;
		if (record != null) {
			if (record.get('leaf')) {
				ExtFrame.info('不能给叶子节点添加子节点！！');
				return;
			}
		}
		var editPageUrl = this.editPage;
		var win = ExtUtil.create(editPageUrl, {
			id : this.id + '-' + editPageUrl.replace(/\./g, ''),
			parentPanel : this,
			selectRecord : record,
			callbackRefresh : function() {
				page.store.load();
			}
		});
		win.show();
	},
	doDelete : function() {
		var panel = this;
		GridPanel.deleteByGrid(this.tree, panel.delete_action);
	},
	doUpdate : function() {
		var page = this;
		var editPageUrl = this.editPage;
//		var record = this.tree.getSelectionModel().getSelection();
		var record =	page.tree.getSelectionModel().lastFocused;
		if (Util.isNull(record)) {
			ExtFrame.info('请选中要编辑的数据！');
		} else {
			var win = ExtUtil.create(editPageUrl, {
				id : this.id + '-' + editPageUrl.replace(/\./g, ''),
				parentPanel : this,
				parentRecord : record,
				callbackRefresh : function() {
					page.store.load();
				}
			});
			win.show();
		}
	}
});